//
//  WWGifCollectionCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/10.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWGifCollectionCell.h"
#import "PublicHeader.h"
#import "WWTextModel.h"
#import "WWTextTableCell.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

@interface WWGifCollectionCell () <UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) MBProgressHUD *progressView;

@end

@implementation WWGifCollectionCell

- (void)getData {
    [AFTools getDataByURLString:[NSString stringWithFormat:@"http://m2.qiushibaike.com/article/list/text?count=30&page=%ld&readarticles=%5B113554390%2C113573077%2C113572016%2C113563262%2C113568254%2C113566089%2C113568484%2C113571960%2C113541904%2C113541375%2C113536607%2C113536645%2C113536968%2C113538042%2C113537564%2C113541341%2C113540408%2C112989773%2C112987851%2C113561959%2C112994307%2C112988628%5D&AdID=14471534548669E1922CE5", _page] WithDataBlock:^(id data) {
        NSArray *tempArray = [NSArray arrayWithArray:[data objectForKey:@"items"]];
        for (NSDictionary *dic in tempArray) {
            WWTextModel *model = [WWTextModel wwModelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        //停止加载
        [self.tableView.mj_footer endRefreshing];
        //停止加载中提示视图
        [_progressView removeFromSuperview];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        self.progressView = [[MBProgressHUD alloc]initWithView:self.tableView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        _page = 1;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWTextTableCell class] forCellReuseIdentifier:@"cell"];
        self.dataArray = [NSMutableArray array];
        [self.tableView NightWithType:UIViewColorTypeNormal];
        [self getData];
        [self.tableView addSubview:_progressView];
        [self.contentView addSubview:_tableView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            //清空原来的数据
            [self.dataArray removeAllObjects];
            _page = 1;
            [self getData];
        }];
        
        //上拉加载
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            //页码加一
            _page++;
            [self getData];
        
        }];
        
    }
    return self;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WWTextTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.dataArray.count) {
        WWTextModel *model = self.dataArray[indexPath.row];
        cell.wModel = model;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count) {
        WWTextModel *model = self.dataArray[indexPath.row];
        return ([model.content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:17]} context:nil].size.height + 50);
    }
    return 0;
}
@end
